package com.dqhl.community.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.dqhl.community.R;
import com.dqhl.community.adapter.ComplainRecordListAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.ComplainRecord;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2016/9/10.
 * 投诉记录
 */
public class ComplainRecordActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivTopBack;
    private TextView tvTopTitle;
    private ListView lvComplainRecord;
    private PtrClassicFrameLayout ptrClassicFrameLayout;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complain_record);

        initView();
        getRecord();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        ivTopBack = (ImageView) findViewById(R.id.iv_top_back);
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        tvTopTitle.setText("投诉记录");
        lvComplainRecord = (ListView) findViewById(R.id.lv_complain_record);
        ptrClassicFrameLayout = (PtrClassicFrameLayout) findViewById(R.id.ptr_list);

        ivTopBack.setOnClickListener(this);

        //下拉刷新
        ptrClassicFrameLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                ptrClassicFrameLayout.autoRefresh(true);
            }
        }, 150);

        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ptrClassicFrameLayout.refreshComplete();
                        //获取数据
//                        getData();
                        toast("已是最新数据了");
                    }
                }, 1000);
            }
        });

        ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        ptrClassicFrameLayout.loadMoreComplete(true);

                    }
                }, 1000);
            }
        });
    }

    /**
     * 获取投诉记录
     */
    private void getRecord() {
        RequestParams params = new RequestParams(Config.Api.complain_record);
        params.addBodyParameter("userid", app.getUserId());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getInfo(result, "data");
                if (errCode == 0) {
                    List<ComplainRecord> complainRecordList = new ArrayList<ComplainRecord>(
                            JSONArray.parseArray(data, ComplainRecord.class)
                    );
                    Dlog.e("complainRecordList:" + complainRecordList.toString());
                    ComplainRecordListAdapter complainRecordListAdapter =
                            new ComplainRecordListAdapter(ComplainRecordActivity.this, complainRecordList);
                    lvComplainRecord.setAdapter(complainRecordListAdapter);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
        }
    }
}
